package com.ikas.ai.server.module.cockpit.controller;

import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.model.PageQuery;
import com.ikas.ai.server.aop.Pageable;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.alarm.service.UnitDeteriorationAlarmService;
import com.ikas.ai.server.module.cockpit.service.CockpitService;
import com.ikas.ai.server.module.machine.service.MachineInfoService;
import com.ikas.ai.utils.PageExUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author tang.xuandong
 * @version 1.0.0
 * @menu 驾驶舱相关接口
 * @date 2023/5/12 9:52
 */
@RestController
@RequestMapping("/cockpit")
@Slf4j
public class CockpitController extends AbstractBean {

    @Resource
    private CockpitService cockpitService;

    @Resource
    private MachineInfoService machineInfoService;

    @Autowired
    private UnitDeteriorationAlarmService unitDeteriorationAlarmService;

    /**
     * 获取上库水位 & 下库水位
     *
     * @return 最新测点数据
     */
    @GetMapping("/getUpAndDownWaterLevel")
    public JsonResult getUpAndDownWaterLevel() {
        return JsonResult.ok(cockpitService.getUpAndDownWaterLevel());
    }


    /**
     * 获取机组实时信息
     *
     * @param machineNo 机组编号（为空则获取所有）
     * @return Json
     */
    @GetMapping("/getMachineInfos")
    public JsonResult getMachineInfos(@RequestParam(required = false) String machineNo) {
        return JsonResult.ok(machineInfoService.getMachineInfos(machineNo));
    }


    /**
     * 状态评估现状总览
     *
     * @return Json
     */
    @GetMapping("/getStateStatistics")
    public JsonResult getStateStatistics() {
        return JsonResult.ok(machineInfoService.getStateStatistics());
    }


    /**
     * 指定时间内机组运行次数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/getMachineStartStatisticsCount")
    public JsonResult getMachineStartStatisticsCount(@RequestParam(required = false) String startDate,
                                                     @RequestParam(required = false) String endDate,
                                                     @RequestParam(required = false) Integer type) {
        return JsonResult.ok(machineInfoService.getMachineStartStatisticsCount(startDate, endDate, type));
    }

    /**
     * 指定时间内机组运行时长
     *
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/getMachineStartStatisticsCost")
    public JsonResult getMachineStartStatisticsCost(@RequestParam(required = false) String startDate,
                                                    @RequestParam(required = false) String endDate,
                                                    @RequestParam(required = false) Integer type) {
        return JsonResult.ok(machineInfoService.getMachineStartStatisticsCost(startDate, endDate, type));
    }

    /**
     * 获取风险处置统计数据
     *
     * @return
     */
    @GetMapping("/getRiskStatistics")
    public JsonResult getRiskStatistics() {
        return JsonResult.ok(unitDeteriorationAlarmService.getUnitAlarmStatistics());
    }


    /**
     * 根据meteId以及开始时间结束时间获取该测点的具体值集合
     *
     * @param startTime
     * @param endTime
     * @param meteId
     * @param meteType
     * @return
     */
    @GetMapping("/getHis")
    public JsonResult getHis(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
            @RequestParam Long meteId,
            @RequestParam Integer meteType,
            PageQuery pageQuery) {
        return JsonResult.ok(cockpitService.getHis(startTime, endTime, meteId, meteType, pageQuery));
    }

    /**
     * 根据机组编号和设备编号，获取其下属的全部测点以及该测点的最新值
     *
     * @param machineNo
     * @param equipmentCode
     * @param meteType      遥信为0，遥测为1
     * @return
     */
    @Pageable(sortBy = "mete_id")
    @GetMapping("/getMeteByEquipmentOrSystem")
    public JsonResult getMeteByEquipmentOrSystem(
            String machineNo,
            String equipmentCode,
            Integer meteType,
            PageQuery pageQuery
    ) {
        return JsonResult.ok(cockpitService.getMeteByEquipmentOrSystemPageable(machineNo, equipmentCode, meteType, pageQuery));
    }

    /**
     * 为下拉框准备的测点模糊查询
     *
     * @param machineNo
     * @param equipmentCode
     * @return
     */
    @GetMapping("/getAllMeteByEquipmentOrSystem")
    public JsonResult getAllMeteByEquipmentOrSystem(
            @RequestParam String machineNo,
            @RequestParam String equipmentCode,
            @RequestParam(required = false) Integer meteType
    ) {
        return JsonResult.ok(cockpitService.getAllMeteByEquipmentOrSystem(machineNo, equipmentCode, meteType));
    }

    /**
     * 获取启停仿真数据
     *
     * @param machineNo
     * @param wfName
     * @param steps     step名称组成的字符串，由英文逗号分隔
     * @return
     */
    @GetMapping("/getStartAndStopSimilar")
    public JsonResult getStartAndStopSimilar(
            @RequestParam String machineNo,
            @RequestParam String wfName,
            @RequestParam String steps
    ) {
        List<String> stepsNames = Arrays.asList(steps.split(","));
        return JsonResult.ok(cockpitService.startAndStopSimilar(machineNo, wfName, stepsNames));
    }

    /**
     * 获取预警数量
     *
     * @param machineNo
     * @return
     */
    @GetMapping("/getAlarmCount")
    public JsonResult getAlarmCount(@RequestParam String machineNo) {
        return JsonResult.ok(cockpitService.getAlarmCount(machineNo));
    }

    /**
     * 驾驶舱启停数据统计
     *
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/startCountStatistics")
    public JsonResult startCountStatistics(@RequestParam(required = false) String startDate,
                                           @RequestParam(required = false) String endDate,
                                           @RequestParam(required = false) Integer type) {
        return JsonResult.ok(cockpitService.startCountStatisticsByPaper(startDate, endDate, type));
    }

    /**
     * 驾驶舱运行数据统计
     *
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/runtimeDataStatistics")
    public JsonResult runtimeDataStatistics(@RequestParam(required = false) String startDate,
                                            @RequestParam(required = false) String endDate,
                                            @RequestParam(required = false) Integer type) {
        return JsonResult.ok(cockpitService.runtimeDataStatistics(startDate, endDate));
    }

}
